unit AM;

interface

uses
  Database,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, Db, DBTables, Menus;

type
  TFormAM = class(TForm)
    BtnOK: TBitBtn;
    BtnCancel: TBitBtn;
    List1: TListBox;
    List2: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    MemoCommands: TMemo;
    LabelCommands: TLabel;
    MainMenu1: TMainMenu;
    Invisible1: TMenuItem;
    Esc1: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure BtnOKClick(Sender: TObject);
    procedure List1KeyPress(Sender: TObject; var Key: Char);
    procedure List1Enter(Sender: TObject);
    procedure List1Exit(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure MemoCommandsClick(Sender: TObject);
    procedure List1Click(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    procedure UpdateCommands;
  public
    { Public declarations }
    am, avoid: string;
    a1m2: integer;
    procedure Setup;
  end;

var
  FormAM: TFormAM;
  which: integer;

implementation
uses DBClient;

{$R *.DFM}

procedure TFormAM.FormCreate(Sender: TObject);
begin
  am:='';
  avoid:='';
  a1m2:=0;
end;

procedure TFormAM.Setup;
var
  table: TClientDataset;
begin
  am:='';
  avoid:='';
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableAliases, false);
    active:=true;
    first;
    while not EOF do begin
      if fieldbyname('Alias').asstring <> avoid then
        List1.items.add(fieldbyname('Alias').asstring);
      next;
    end;
    active:=false;
    cloneCursor(Datawell.TableMacros, false);
    active:=true;
    first;
    while not EOF do begin
      if (fieldbyname('Macro').asstring <> avoid) and
         (fieldbyname('Macro').asstring <> 'Alt+F4') and
         (fieldbyname('Macro').asstring <> 'Alt+F6') and
         (fieldbyname('Macro').asstring <> 'F7') and
         (fieldbyname('Macro').asstring <> 'Shift+F7') then
        List2.items.add(fieldbyname('Macro').asstring);
      next;
    end;
  end;
  table.free;
  if List1.items.count < 1 then begin
    List1.enabled:=false;
    List1.color:=clsilver;
  end;
  if List2.items.count < 1 then begin
    List2.enabled:=false;
    List2.color:=clsilver;
  end;
end;

procedure TFormAM.BtnCancelClick(Sender: TObject);
begin
  close;
end;

procedure TFormAM.List1KeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnOK.click;
  end;
end;

procedure TFormAM.BtnOKClick(Sender: TObject);
var i: integer;
begin
  if which = 1 then begin
    i:=List1.itemindex;
    if i < 0 then exit;
    am:=List1.items[i];
    a1m2:=1;
  end
  else begin
    i:=List2.itemindex;
    if i < 0 then exit;
    am:=List2.items[i];
    a1m2:=2;
  end;
  close;
end;

procedure TFormAM.List1Enter(Sender: TObject);
begin
  TListBox(sender).color:=clwindow;
  which:=TComponent(sender).tag;
end;

procedure TFormAM.List1Exit(Sender: TObject);
begin
  TListBox(sender).color:=clsilver;
end;

procedure TFormAM.MemoCommandsClick(Sender: TObject);
begin
  case which of
    0: BtnOK.setfocus;
    1: List1.setfocus;
    2: List2.setfocus;
  end;
end;

procedure TFormAM.List1Click(Sender: TObject);
begin
  UpdateCommands;
end;

procedure TFormAM.FormShow(Sender: TObject);
begin
  if List1.enabled then begin
    List1.setfocus;
    List1.itemindex:=1;
    List1.itemindex:=0;
    which:=1;
  end
  else if List2.enabled then begin
    List2.setfocus;
    List2.itemindex:=1;
    List2.itemindex:=0;
    which:=2;
  end
  else begin
    messagedlg('There are no aliases or macros defined',mtinformation,[mbok],0);
    BtnOK.enabled:=false;
    BtnCancel.setfocus;
    which:=0;
    exit;
  end;
  UpdateCommands;
end;

procedure TFormAM.UpdateCommands;
var
  table: TClientDataset;
  master: TCustomClientDataset;
  cname, am: string;
  i: integer;
  didfind: boolean;
begin
  MemoCommands.lines.clear;
  case which of
    1: begin
         master := Datawell.TableAliases;
         cname:='Alias';
         i:=List1.itemindex;
         if i < 0 then begin
           LabelCommands.caption:='No Alias/Macro Selected';
           exit;
         end;
         am:=List1.items[i];
       end;
    2: begin
         master := Datawell.TableMacros;
         cname:='Macro';
         i:=List2.itemindex;
         if i < 0 then begin
           LabelCommands.caption:='No Alias/Macro Selected';
           exit;
         end;
         am:=List2.items[i];
       end;
    else raise Exception.CreateFmt('TFormAM.UpdateCommands: Invalid selector: %d', [which]);
  end;
  table:=TClientDataset.create(self);
  with table do begin
    cloneCursor(master, false);
    filter := 'Master = '+Datawell.masterindex;
    filtered := true;
    active:=true;
    didfind:=false;
    first;
    while not EOF do begin
      if fieldbyname(cname).asstring = am then begin
        didfind:=true;
        break;
      end;
      next;
    end;
  end;
  if didfind then MemoCommands.lines.assign(table.fieldbyname('Script'));
  table.free;
  LabelCommands.caption:='Commands Assigned to the '+cname+' ('+inttostr(MemoCommands.lines.count)+')';
  if MemoCommands.lines.count < 1 then MemoCommands.lines.add('<<No Commands Assigned>>');
end;

procedure TFormAM.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key = vk_Up) or (key = vk_Down) or (key = vk_Tab) then UpdateCommands;
end;

end.
